package com.example.myapplication.tack_05;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_05.data.LogOnData;
import com.example.myapplication.utils.OkHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

public class Exit extends AppCompatActivity {
    private ImageView exit;
    private EditText username;
    private EditText password;
    private TextView login;
    private JSONObject jsonObject;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private String name;
    private String pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_05__exit);
        init();
        exit_();
        //登录
        login_();

    }

    private void login_() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                name = username.getText().toString();
                pwd = password.getText().toString();
                try{
                    jsonObject.put("username", name);
                    jsonObject.put("password", pwd);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String json = jsonObject.toString();
                        LogOn(json);
                    }
                });
            }
        });
    }

    private void LogOn(String json) {
        String url = "/prod-api/api/login";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, LogOnData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                LogOnData logOnData = (LogOnData) obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LogOnAdapter(logOnData);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    @SuppressLint("LongLogTag")
    private void LogOnAdapter(LogOnData logOnData) {
        if(logOnData.getCode() == 200){
            Toast.makeText(Exit.this,"登录成功",Toast.LENGTH_SHORT).show();
            String token = logOnData.getToken();
            editor.putString("token", token);
            editor.putString("username",name);
            editor.putString("password",pwd);
            Log.e("token=======================================",token);
            editor.apply();
            OkHttpTool.TOKEN = sharedPreferences.getString("token", "");
            Exit.this.finish();
        }else{
            Toast.makeText(Exit.this,"用户不存在/密码错误",Toast.LENGTH_SHORT).show();
        }
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Exit.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        jsonObject = new JSONObject();
        sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        if("".equals(OkHttpTool.TOKEN)){
            name = sharedPreferences.getString("username", "");
            pwd = sharedPreferences.getString("password", "");
            username.setText(name);
            password.setText(pwd);
        }


    }
}